﻿using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class Yahoo
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

          
          
            string urlGoc = "https://vn.nang.yahoo.com";


          //  ListCate.AddRange(load.LoadCatesNoSub("https://vn.nang.yahoo.com/thoitrang/", "//ul//li[@id='yui_3_9_1_1_1398516153089_123']", urlGoc, manuId, "//a"));

         //   ListCate.AddRange(load.LoadCatesNoSub("https://vn.nang.yahoo.com", "//ul//li[@id='yui_3_9_1_1_1398516153089_92']", urlGoc, manuId, "//a"));


            string[] listUrlCategory = { "/lamdep/" };
            string[] nameCate = { "Làm đẹp"};
            string[] posCate = { "5" };
            for (int h = 0; h < listUrlCategory.Count(); h++)
            {
                string itemPage = urlGoc + listUrlCategory[h];
                string name = nameCate[h];
                ListCate.AddRange(load.LoadCatesWithSub(itemPage, "//div[@class='nav-stack nav-0']//ul[@class='navlist yog-grid']//li",
                    "//div[@class='nav-stack nav-1']//ul", urlGoc, manuId, "//li", "//a", "//a",posCate[h], true));
            }
           

            
            
            int dem = 0;



            for (int i = 0; i < ListCate.Count(); i++)
            {
               
                

                 


                 //   p.Url = firstPage.Url + "?page=" + trang;

                  List<NewsItemModel> ListFeaturedNews = new List<NewsItemModel>();
                        var loadFeatured1 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='yog-col yog-11u yom-primary']","//h2//a", "", "//h2//a", urlGoc);
                        var loadFeatured2 = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@class='yog-col yog-5u yog-col-last']//div[@class='x2']", "//h2//a", "", "//h2//a", urlGoc);
                        ListFeaturedNews.AddRange(loadFeatured1);
                        ListFeaturedNews.AddRange(loadFeatured2);

                        for (int k = 0; k < ListFeaturedNews.Count(); k++)
                        {
                            var item = load.LoadNews(ListFeaturedNews[k],"//div[@class='bd']", "//div[@class='bd']//strong", manuId,0, "//div[@class='bd']//img", "",false);
                            if (item != null)
                            {
                                dem++;
                                item.Hot = "Tin hot";
                                load.AddItemToListView(item);
                                Extensions.ImageFromURL(item.UrlFirstImage).SaveImageToFolder(folder + "/Featured" + "/" + item.Id + " _FirstImage.jpg");

                            }

                            //if (dem == 30)
                            //    break;
                        }
                    
                   
                    var ListDetail = load.LoadUpdateNewHomeNews(ListCate[i], "//ul[@class='yom-list-wide thumbnail']//li", "//div[@class='txt']//h4//a", "", "//div[@class='txt']//h4//a", urlGoc);

                  //  Boolean flag = true;

                

                    // phai chay dong for nguoc, de xet phan tu dau tien

                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = load.LoadNews(ListDetail[k], "//div[@class='bd']", "//div[@class='bd']//strong", manuId, 0,"//div[@class='bd']//img", "",true
                            );
                        if (item != null)
                        {
                        
                                dem++;

                                load.AddItemToListView(item);
                                Extensions.ImageFromURL(item.UrlFirstImage).SaveImageToFolder(folder + "/News" + "/" + item.Id + " _FirstImage.jpg");


                            
                            
                        }
                      


                    }

            
            }
            System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
